package cn.kt.service.impl;

import cn.kt.dao.MenuDao;
import cn.kt.pojo.menu.MenuListPojo;
import cn.kt.pojo.menu.MenuPojo;
import cn.kt.service.IndexMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndexMenuServiceImpl implements IndexMenuService {

    @Autowired
    MenuDao menuDao;

    @Override
    public List<MenuListPojo> list() {
        //接收一级菜单
        List<MenuListPojo> pMenuList = menuDao.findLive_1MenuList();
        //接收二级菜单
        List<MenuPojo> sMenuList = menuDao.findLive_2MenuList();
        //遍历菜单，按照一级菜单的id 二级菜单的pid 装填二级菜单到一级菜单中
        for (MenuListPojo mlp: pMenuList) {
            List<MenuPojo> mplist = new ArrayList<>();
            for (MenuPojo mp:sMenuList) {
                if (mlp.getId() == mp.getPid()){
                    mplist.add(mp);
                }
            }
            mlp.setList(mplist);
        }
        return pMenuList;
    }
}
